package com.sto.stoapp.activity.sales;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.activity.scanner.business.BusinessListActivity;
import com.sto.stoapp.adapter.SoleAdapter;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.dataprocess.parcelable.SelectTableParcel;
import com.sto.stoapp.db.constant.StoOrderReasonConstant;
import com.sto.stoapp.db.manager.SqlSelectTableManager;
import com.sto.stoapp.entity.FunctionEntity;
import com.sto.stoapp.listener.StoOnItemClickListener;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.widget.StoHomeActivity;

import java.util.ArrayList;
import java.util.List;

public class SalesOrderFunctionActivity extends StoHomeActivity {

    // 按键映射
    private static final int[] MAPP_TO_KEY = { R.string.order_piece_view,
            R.string.order_back_cause, R.string.auto_down_timer };
    private ListView OrderFuncList;
    private SoleAdapter OrderListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sto_sales_order_function);
        this.setTitleBottom(R.string.order_function,
                HomeBroadAction.SALES_MAN_BROAD);

        initWidget();
    }

    private void initWidget() {
        OrderFuncList = (ListView) findViewById(R.id.receivedFunc_list);
        OrderListAdapter = new SoleAdapter(this, getResourceList());
        OrderFuncList.setAdapter(OrderListAdapter);

        OrderFuncList.setOnItemClickListener(new StoOnItemClickListener(this) {

            @Override
            public void KcOnItemClickListener(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                arg1.setSelected(true);
                TextView title = (TextView) arg1.findViewById(R.id.res_test);
                clickItemEvent(title.getText().toString());
            }
        });

    }

    private void clickItemEvent(String title) {
        // 订单查看
        if (title.equals(getText(R.string.order_piece_view))) {
            Intent intent = new Intent(this, SalesOrderViewActivity.class);

            //			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        /*// 手动下载订单
        else if (title.equals(getText(R.string.down_order_piece))) {

        	if (StoFilePreference.getInstance(this).getOrderFunctionControl()) {
        		Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
        		downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
        				new int[] { MessageType.ORDER_DOWNLOAD_TYPE });
        		sendBroadcast(downIntent);
        	} else {
        		Helper.showToast(this, getText(R.string.order_function_no_open)
        				.toString());
        	}
        }*/
        // 订单打回原因
        else if (title.equals(getText(R.string.order_back_cause))) {
            try {
                SelectTableParcel tableParcel = new SelectTableParcel();
                tableParcel
                        .setSelectSql(SqlSelectTableManager.SQL_ORDER_REASON_ALL);
                tableParcel.setSelectField(new String[] {});

                tableParcel
                        .setSelectCode(StoOrderReasonConstant.ORDER_SERIAL_NUMBER);
                tableParcel
                        .setSelectName(StoOrderReasonConstant.RECORD_CONTENT);
                tableParcel.setProperty(getString(R.string.order_back_reason));

                Intent intent = new Intent(SalesOrderFunctionActivity.this,
                        BusinessListActivity.class);
                intent.putExtra("businessListData", tableParcel);

                startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
                ExceptionLog.writeLog("clickItemEvent", e.getMessage());
            }
        }
        // 自动下载时间设置
        else if (title.equals(getText(R.string.auto_down_timer))) {
            Intent intent = new Intent(this,
                    SalesOrderAutoDownTimerSetActivity.class);
            //			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        } else {

        }
    }

    //	private void ShowOrderCause() {
    //		/*
    //		 * try { SelectTableParcel tableParcel = new SelectTableParcel();
    //		 * tableParcel.setSelectSql(SqlSelectTableManager.SQL_PROVINCE_ALL);
    //		 * tableParcel.setSelectField(new String[] {});
    //		 * tableParcel.setSelectCode("serial_number");
    //		 * tableParcel.setSelectName("province_info");
    //		 * tableParcel.setProperty(null);
    //		 * 
    //		 * Intent intent = new Intent(SalesOrderFunctionActivity.this,
    //		 * StoSelectTableActivity.class); intent.putExtra("tableData",
    //		 * tableParcel);
    //		 * 
    //		 * startActivityForResult(intent, DESTINATION_RESULT); } catch
    //		 * (Exception e) { e.printStackTrace(); }
    //		 */
    //	}

    private List<FunctionEntity> getResourceList() {
        List<String> resList = new ArrayList<String>();
        resList.add(getText(R.string.order_piece_view).toString());
        //resList.add(getText(R.string.down_order_piece).toString());
        resList.add(getText(R.string.order_back_cause).toString());
        resList.add(getText(R.string.auto_down_timer).toString());

        List<FunctionEntity> functionList = new ArrayList<FunctionEntity>();
        for (String res : resList) {
            FunctionEntity entity = new FunctionEntity();
            entity.setTitle(res);
            functionList.add(entity);
        }
        return functionList;
    }



    @Override
    protected void onDestroy() {
    	OrderFuncList = null;
        super.onDestroy();
    }


}
